package com.example.dragandview.seniorUI;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;

import com.example.dragandview.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GridViewActivity extends Activity {
    private int[] arrayImages = new int[]{R.drawable.meinv1,
            R.drawable.meinv2,R.drawable.meinv3,R.drawable.meinv4};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.seniorui_gridview);
        GridView gridView = findViewById(R.id.gridlayout);
//        List<Map<String,Object>> listitem = new ArrayList<>();
//        for (int i = 0 ; i< arrayImages.length ; i++){
//            Map<String,Object> map = new HashMap<>();
//            map.put("image",arrayImages[i]);
//            listitem.add(map);
//        }
//        SimpleAdapter simpleAdapter = new SimpleAdapter(this,listitem,R.layout.seniorui_gridview_cell,new String[]{"image"},new int[]{R.id.imageview_gridview});
//        gridView.setAdapter(simpleAdapter);

        gridView.setAdapter(new ImageAdaptar(this));

    }
    public class ImageAdaptar extends BaseAdapter{
        private Context context;
        public ImageAdaptar(Context c){
            context = c;
        }
        @Override
        public int getCount() {
            return arrayImages.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView ;
            if(convertView == null){
                imageView = new ImageView(context);
                imageView.setLayoutParams(new GridView.LayoutParams(250,300));
                imageView.setScaleType(ImageView.ScaleType.CENTER);
            }else {
                imageView = (ImageView) convertView;
            }
            imageView.setImageResource(arrayImages[position]);
            return imageView;
        }
    }
}
